<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>分类新增</title>
		<link href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.css" rel="stylesheet">
		<link href="https://cdnjs.cloudflare.com/ajax/libs/layer/2.3/skin/layer.css" rel="stylesheet">
		<link href="../static/css/index.css" rel="stylesheet" />
	</head>

	<body>

		<!--导航-->
		<!--<nav class="ui inverted attached segment i-padded-tb-mini i-shadow-small">
			<div class="ui container">
				<div class="ui inverted secondary stackable menu">
					<h2 class="ui teal header item">管理后台</h2>
					<a href="blogs.html" class=" i-item item i-mobile-hide"><i class="mini home icon"></i>博客</a>
					<a href="types.html" class="active i-item item i-mobile-hide"><i class="mini idea icon"></i>分类</a>
					<a href="tags.html" class="i-item item i-mobile-hide"><i class="mini tags icon"></i>标签</a>
					<div class="right i-item i-mobile-hide menu">
						<div class="ui dropdown  item">
							<div class="text">
								<img class="ui avatar image" src="https://unsplash.it/100/100?image=1005"> 李仁密
							</div>
							<i class="dropdown icon"></i>
							<div class="menu">
								<a href="#" class="item">注销</a>
							</div>
						</div>
					</div>
				</div>
			</div>
			<a href="#" class="ui menu toggle black icon button i-right-top i-mobile-show">
				<i class="sidebar icon"></i>
			</a>
		</nav>-->
		<div id="head"></div>
		<div class="ui attached pointing menu">
			<div class="ui container">
				<div class="right menu">
					<a href="types-input.html" class="active item">新增</a>
					<a href="types.html" class="teal  item">列表</a>
				</div>
			</div>
		</div>

		<!--中间内容-->
		<div class="i-container-small i-padded-tb-big">
			<div class="ui container" id="typeForm">
				<form action="#" method="post" class="ui form">
					<input type="hidden" name="id">
					<div class=" field">
						<div class="ui left labeled input">
							<label class="ui teal basic label">名称</label>
							<input type="text" name="name" placeholder="分类名称" v-model="type.name" class="Required" id="name">
						</div>
					</div>

					<div class="ui mini negative message" style="display: none;" id="checkForm">请检查必填项</div>

					<div class="ui right aligned container">
						<button type="button" class="ui button" onclick="window.history.go(-1)">返回</button>
						<button class="ui teal submit button" v-on:click="submit()" type="button">提交</button>
					</div>

				</form>
			</div>
		</div>

		<br>
		<br>
		<br>
		<br>
		<br>
		<br>
		<br>
		<br>
		<!--底部footer-->
		<div id="foot"></div>

		<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.js"></script>
		<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/layer/2.3/layer.js"></script>
		<!-- axios -->
		<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
		<script src="../static/js/function.js"></script>
		<script>
			$('.menu.toggle').click(function() {
				$('.i-item').toggleClass('i-mobile-hide');
			});

			$('.ui.dropdown').dropdown({
				on: 'hover'
			});

			$('.ui.form').form({
				fields: {
					title: {
						identifier: 'name',
						rules: [{
							type: 'empty',
							prompt: '请输入分类名称'
						}]
					}
				}
			});

			var pageType = '';
			var id = '';
			var type = {
				'name': '',
				'id': '',
			}
			$(function() {
			$('#head').load('../common/head-admin.html',null,function(){
				$('.navbarlist a').removeClass('active');
				$('.navbarlist a:eq(1)').addClass('active');
			});
			$('#foot').load('../common/foot.html');
				pageType = getQueryVariable("type");
				id = getQueryVariable("id");
				if(pageType == 'edit') {
					typeDetail();
				}
			})

			function typeDetail() {
				$.ajax({
					type: "get",
					url: urlPath + "/type/api/edit_type",
					data: {
						'id': id
					},
					success: function(res) {
						$('#name').val(res.data.name);
					}
				});
			}

			var vue = new Vue({
				el: "#typeForm",
				data: {
					type
				},
				methods: {
					submit: function() {
						if(pageType == 'add') {
							checkRequired(type);
							$.ajax({
								type: "post",
								contentType: 'application/json',
								url: urlPath + "/type/api/save_type",
								data: JSON.stringify(type),
								success: function(res) {
									if(res.code === 200) {
										propSuccess(res, function() {
											window.location.href = "types.html";
										})
									} else {
										
									}
								}
							});
						} else if(pageType == 'edit') {
							type.name = $('#name').val().trim();
							type.id = id;
							checkRequired(type);
							$.ajax({
								type: "post",
								url: urlPath + "/type/api/update_type",
								contentType: 'application/json',
								async: true,
								data: JSON.stringify(type),
								success: function(res) {
									if(res.code === 200) {
										propSuccess(res, function() {
											window.location.href = "types.html";
										})
									}else{
										propFailReload(res, function() {
											window.location.reload();
										})
									}
								}
							});
						}
					}
				},
			})
		</script>
	</body>

</html>